package com.starwish.starwishblog.advice;


import com.starwish.starwishblog.entity.Manager;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.ManagerService;
import com.starwish.starwishblog.vo.AccountVo;
import com.starwish.starwishblog.vo.ManagerLoginVo;
import com.starwish.starwishblog.vo.ManagerOperationVo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.lang.reflect.Method;
import java.util.Date;

@Component    //把这个类交给 容器实例化
@Aspect      //表示这个类是一个 切面类
public class ManagerAdvice<T> {
    private Integer managerId;
    private String adminName;
    private String methodName;
    private String operationName;

    @Autowired
    ManagerService managerService;



    @Pointcut("execution(* com.starwish.starwishblog.web.back.MangerController.login(..))")
    public void initInfo(){

    }
    @Before("initInfo()")
    public void initInfo(JoinPoint joinPoint){
            AccountVo accountVo = (AccountVo) joinPoint.getArgs()[0];
            adminName = accountVo.getAdminName();
            System.out.println("dsasd"+adminName);
            Manager manager = managerService.login(adminName);
            if(manager !=null){
            managerId = manager.getId();
        }

    }

    @AfterReturning(value = "execution(* com.starwish.starwishblog.web.back.*.*(..))",returning = "methodResult")
    public void afterReturning(JoinPoint joinPoint,Object methodResult){
        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        Method method = ms.getMethod();
        String className1 = joinPoint.getTarget().getClass().toString();
        ResultResponse<T> resultResponse = (ResultResponse<T>) methodResult;
        operationName = className1.substring(className1.lastIndexOf(".")+1)+"==>"+method.getName();

        System.out.println(className1.substring(className1.lastIndexOf(".")+1)+method.getName());
        if("MangerController==>login".equals(operationName)){

            System.out.println("登录信息"+resultResponse.getMsg());
            ManagerLoginVo managerLoginVo = new ManagerLoginVo();
            managerLoginVo.setManagerId(managerId);
            managerLoginVo.setAdminName(adminName);
            managerLoginVo.setCreateTime(new Date());
            managerLoginVo.setOperationType(operationName);
            managerLoginVo.setResult(resultResponse.getMsg());
            int i = managerService.addManagerLogin(managerLoginVo);
            System.out.println("后台登录管理"+operationName);

        }else if("MangerController==>loginOut".equals(operationName)){
            System.out.println("登录信息"+resultResponse.getMsg());
            ManagerLoginVo managerLoginVo = new ManagerLoginVo();
            managerLoginVo.setManagerId(managerId);
            managerLoginVo.setAdminName(adminName);
            managerLoginVo.setCreateTime(new Date());
            managerLoginVo.setOperationType(operationName);
            managerLoginVo.setResult(resultResponse.getMsg());
            int i = managerService.addManagerLogin(managerLoginVo);
            System.out.println("后台登录管理"+operationName);
        }else {
            ManagerOperationVo operationVo = new ManagerOperationVo();
            operationVo.setManagerId(managerId);
            operationVo.setAdminName(adminName);
            operationVo.setCreateTime(new Date());
            operationVo.setOperationType(operationName);
            operationVo.setResult(resultResponse.getMsg());
            operationVo.setModifyTime(new Date());
            operationVo.setIsDelete("0");
            int i = managerService.addManagerOperation(operationVo);
            System.out.println(operationName+resultResponse.getMsg());
        }
    }

}
